public class ThreadDemo_线程死锁 {
    public static void main(String[] args) {
        Object lockA = new Object();
        Object lockB = new Object();
        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockA) {
                    System.out.println("线程1 获得锁A");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程1 等待锁B");
                    synchronized (lockB) {
                        System.out.println("线程1 获得锁B");
                    }
                }
            }
        });
        t1.start();
// 创建线程 2
        Thread t2 = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lockA) {
                    System.out.println("线程2 获得锁A");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("线程2 等待锁B");
                    synchronized (lockB) {
                        System.out.println("线程2 获得锁B");
                    }
                }
            }
        });
        t2.start();
    }
}
